package com.dubbo;

import com.alibaba.dubbo.common.logger.Logger;
import com.alibaba.dubbo.common.logger.LoggerFactory;
import com.google.common.util.concurrent.AbstractIdleService;
import org.springframework.context.support.ClassPathXmlApplicationContext;


/**
 * Created by wxy on 2016/7/28.
 */
public class Bootstrap extends AbstractIdleService {

    private static final Logger log = LoggerFactory.getLogger(Bootstrap.class);

    private ClassPathXmlApplicationContext context;

    public static void main(String[] args) {
        Bootstrap coreManagerStart = new Bootstrap();
        coreManagerStart.startAsync();
        try {
            Object lock = new Object();
            synchronized (lock){
                while(true){
                        lock.wait();
                }
            }
        } catch (Exception e) {
            log.error("ignore interruption");
        }
    }

    public static void start(String[] args) {
        main(args);
    }

    public static void stop(String[] args) {
        System.exit(0);
    }

    @Override
    protected void startUp() throws Exception {
        context = new ClassPathXmlApplicationContext(new String[]{"spring-context.xml"});
        context.start();
        context.registerShutdownHook();
        log.info("service started successfully");
    }

    @Override
    protected void shutDown() throws Exception {
        context.stop();
        log.info("service stopped successfully");
    }
}
